package com.abvnet.project.water.api.web;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectTest;
import com.abvnet.project.water.project.service.IProjectTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/project/small")
public class ApiProjectTestController extends BaseController {

    @Autowired
    private IProjectTestService projectTestService;

    @GetMapping("/get")
    public AjaxResult get(@RequestParam("projectId") Long projectId) {
        return AjaxResult.success(projectTestService.selectProjectTestByProjectId(projectId));
    }

    @PostMapping("/edit")
    public AjaxResult editSave(ProjectTest projectTest, @RequestParam(name = "testFile",
            required = false) MultipartFile testFile) throws IOException {
        if(StringUtils.isNotEmpty(projectTest.getTestDelete())) {
            if(StringUtils.equals("1",projectTest.getTestDelete())) {
                projectTest.setTestUrl(StringUtils.EMPTY);
            }
        }
        if (testFile != null && !testFile.isEmpty()) {
            String testUrl = FileUploadUtils.upload(AbvnetConfig.getSmallPath(), testFile);
            projectTest.setTestUrl(testUrl);
        }
        return toAjax(projectTestService.updateProjectTest(projectTest));
    }
}